define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree','fileinput','vue'], function ($, undefined, Backend, Table, Form,undefined,fileinput,Vue) {
//读取选中的条目
    $.jstree.core.prototype.get_all_checked = function (full) {
        var obj = this.get_selected(), i, j;
        for (i = 0, j = obj.length; i < j; i++) {
            obj = obj.concat(this.get_node(obj[i]).parents);
        }
        obj = $.grep(obj, function (v, i, a) {
            return v != '#';
        });
        obj = obj.filter(function (itm, i, a) {
            return i == a.indexOf(itm);
        });
        return full ? $.map(obj, $.proxy(function (i) {
            return this.get_node(i);
        }, this)) : obj;
    };
    function initImageFileInput(ctrlName,uploadUrl){
        $('#upload_image_file').fileinput('refresh',{
            language: 'zh', //设置语言
            uploadUrl: uploadUrl, //上传的地址
            allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀
            showUpload: true, //是否显示上传按钮
            showRemove:false,
            showCaption: false,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            initialPreviewConfig:[],
            previewFileIcon: "<i  class='glyphicon glyphicon-king'></i>",
        }).on("fileuploaded", function(event, data) {
            console.info(ctrlName);
            console.info(data);
            if(data.response.code == 1){
                Toastr.success(data.response.msg);
                $('#'+ctrlName).val(data.response.data.url);
            }else{
                Toastr.error(data.response.msg);
            }
        }).on("filesuccessremove", function (event, data, previewId, index) {
            return false;
        });
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/index',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search:false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'nickname', title: __('Nickname'),operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'status', title: __("Status"), formatter: Table.api.formatter.status,searchable:false},
                        {field: 'answer_money', title: __("Answer money"),searchable:false},
                        {field: 'balance', title: __("Balance"),searchable:false},
                        {field: 'last_one', title: __("Last one"),searchable:false},
                        {field: 'is_freeze', title: __('Is freeze'),formatter:function(value,row,index){
                            value = value + '';
                            switch (value){
                                case '0':
                                    return __('Not freeze');
                                case '1':
                                    return __('Freeze');
                                default:
                                    return __('Not freeze');
                            }
                        },searchList: {'0': __('Not freeze'), '1': __('Freeze')}, style: 'min-width:100px;'
                        },
                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime,searchable:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'freeze',
                                text: __('Freeze function'),
                                icon: 'fa fa-check',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'user/freeze'
                            },
                                {
                                    name: 'send',
                                    text: __('Send list'),
                                    icon: 'fa fa-detail',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/send_list'
                                }],
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        freeze:function(){
            Controller.api.bindevent();
        },
        add:function () {
            Form.api.bindevent($("form[role=form]"));
            var upload_image_url = Config.site.cdnurl+'/index.php/admin/user/uploadImageFile';
            initImageFileInput('upload_image_path',upload_image_url);
        },
        edit:function () {
            Form.api.bindevent($("form[role=form]"));
            var upload_image_url = Config.site.cdnurl+'/index.php/admin/user/uploadImageFile';
            initImageFileInput('upload_image_path',upload_image_url);
        },
        send:function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = new Vue({
                el:"#form",
                data:{
                    standard_list: [],
                    standard_id: 0
                },
                watch:{
                },methods:{

                }
            });
            function getStandardList() {
                $.get("user/getStandardList",function(result) {
                    vue.standard_list = result;
                });
            }
            getStandardList();
        },
        robot_list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/robot_list',
                    edit_url: 'user/edit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: '/admin/user/robot_list',
                search:false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'nickname', title: __('Nickname'),operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'send',
                                    text: __('Send list'),
                                    icon: 'fa fa-detail',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/send_list'
                                }],
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        send_list:function () {
            var user_id = Config.user_id;
            Table.api.init({
                extend: {
                    index_url: 'user/send_list/ids/'+user_id
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: '/admin/user/send_list/ids/'+user_id,
                search:false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'nickname', title: __('Nickname'),searchable:false},
                        {field: 'money', title: __("Money"),searchable:false},
                        {field: 'answer_money', title: __("Answer money"),searchable:false},
                        {field: 'status', title: __('Status'),formatter:function(value,row,index){
                            value = value + '';
                            switch (value){
                                case '0':
                                    return __('Wait');
                                case '1':
                                    return __('Answer in');
                                case '2':
                                    return __('Withdraw');
                                case '3':
                                    return __('Receive');
                                default:
                                    return __('Not freeze');
                            }
                        },searchList: {'0': __('Wait'), '1': __('Answer in'), '2': __('Withdraw'), '3': __('Receive')}, style: 'min-width:100px;'
                        },
                        {field: 'is_pay', title: __('Is pay'),formatter:function(value,row,index){
                            value = value + '';
                            switch (value){
                                case '0':
                                    return __('Not pay');
                                case '1':
                                    return __('Pay');
                                default:
                                    return __('Not pay');
                            }
                        },searchList: {'0': __('Not pay'), '1': __('Pay')}, style: 'min-width:100px;'
                        },
                        {field: 'is_visible', title: __('Is visible'),formatter:function(value,row,index){
                            value = value + '';
                            switch (value){
                                case '0':
                                    return __('Not visible');
                                case '1':
                                    return __('Visible');
                                default:
                                    return __('Not visible');
                            }
                        },searchList: {'0': __('Not visible'), '1': __('Visible')}, style: 'min-width:100px;'
                        },
                        {field: 'create_time', title: __('Create time'), formatter: Table.api.formatter.datetime,searchable:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        }
        ,api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    if ($("#treeview").size() > 0) {
                        var r = $("#treeview").jstree("get_all_checked");
                        $("input[name='row[rules]']").val(r.join(','));
                    }
                    return true;
                });
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });
                function getNodeList() {
                    var user_id = Config.user_id;
                    $.ajax({
                        url: "user/node_list",
                        type: 'post',
                        dataType: 'json',
                        data: {id: user_id},
                        success: function (ret) {
                            if (ret.hasOwnProperty("code")) {
                                var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
                                if (ret.code === 1) {
                                    //销毁已有的节点树
                                    $("#treeview").jstree("destroy");
                                    Controller.api.rendertree(data);
                                } else {
                                    Backend.api.toastr.error(ret.data);
                                }
                            }
                        }, error: function (e) {
                            Backend.api.toastr.error(e.message);
                        }
                    });
                }
                getNodeList();
            },
            rendertree: function (content) {
                $("#treeview")
                    .on('redraw.jstree', function (e) {
                        $(".layer-footer").attr("domrefresh", Math.random());
                    })
                    .jstree({
                        "themes": {"stripes": true},
                        "checkbox": {
                            "keep_selected_style": false,
                        },
                        "types": {
                            "root": {
                                "icon": "fa fa-folder-open",
                            },
                            "menu": {
                                "icon": "fa fa-folder-open",
                            },
                            "file": {
                                "icon": "fa fa-file-o",
                            }
                        },
                        "plugins": ["checkbox", "types"],
                        "core": {
                            'check_callback': true,
                            "data": content
                        }
                    });
            }
        }
    };
    return Controller;
});